package org.example.queueadmin.task;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Component
@RequiredArgsConstructor
public class TaskManager {
  private final Map<Long, TaskRunner> runners = new ConcurrentHashMap<>();

  public synchronized void register(Long taskId, TaskRunner runner) {
    runners.put(taskId, runner);
  }

  public synchronized void unregister(Long taskId) {
    TaskRunner r = runners.remove(taskId);
    if (r != null) {
      r.stop();
    }
  }

  public TaskRunner get(Long taskId) {
    return runners.get(taskId);
  }
}

